package sch.guo.crmsys.controller.cop;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import sch.guo.crmsys.base.ResultUtil;
import sch.guo.crmsys.base.dto.ResultDTO;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import sch.guo.crmsys.dto.cop.OrderDetailDTO;
import sch.guo.crmsys.entity.cop.CopOrder;
import sch.guo.crmsys.service.cop.CopOrderDetailService;
import sch.guo.crmsys.entity.cop.CopOrderDetail;
import sch.guo.crmsys.service.cop.CopOrderService;

/**
 * @since 2019-12-31
 */
@RestController
@RequestMapping("/copOrderDetail")
public class CopOrderDetailController {

    @Autowired
    private CopOrderDetailService copOrderDetailService;
    @Autowired
    private CopOrderService copOrderService;

    @GetMapping()
    @ApiOperation("根据订单编号查询")
    public ResultDTO selectByOrderId(Long orderId) throws Exception{
        List<OrderDetailDTO> orderDetailDTOS = copOrderDetailService.selectByOrderId(orderId);
        return ResultUtil.Success(orderDetailDTOS,orderDetailDTOS.size());
    }

    @GetMapping("/{id:\\d+}")
    @ApiOperation("ID查询")
    public ResultDTO selectEntityPage(@PathVariable Long id) throws Exception{
        return ResultUtil.Success(copOrderDetailService.getById(id));
    }

    @PostMapping
    @ApiOperation(value = "插入")
    @Transactional
    public ResultDTO insertOne(@RequestBody CopOrderDetail copOrderDetail) throws Exception {
        CopOrder order = copOrderService.getById(copOrderDetail.getOrderId());
        order.setOrderTotalPrice(order.getOrderTotalPrice().add(copOrderDetail.getOrderDetailPrice()));
        copOrderService.updateById(order);

        copOrderDetailService.save(copOrderDetail);
        return ResultUtil.Success(copOrderDetail);
    }

    @PutMapping
    @ApiOperation(value = "按ID修改")
    @Transactional
    public ResultDTO updateById(@RequestBody CopOrderDetail copOrderDetail) throws Exception {

        CopOrderDetail oldDetail = copOrderDetailService.getById(copOrderDetail.getOrderDetailId());
        BigDecimal price = copOrderDetail.getOrderDetailPrice().subtract(oldDetail.getOrderDetailPrice());

        CopOrder order = copOrderService.getById(copOrderDetail.getOrderId());
        order.setOrderTotalPrice(order.getOrderTotalPrice().add(price));
        copOrderService.updateById(order);

        copOrderDetailService.updateById(copOrderDetail);
        return ResultUtil.Success();
    }

    @DeleteMapping("/{id:\\d+}")
    @ApiOperation("按ID删除")
    public ResultDTO deleteById(@PathVariable Long id) throws Exception {
        copOrderDetailService.removeById((long) id);
        return ResultUtil.Success();
    }

}
